package com.cgy.wodeyigui.ware.service.impl;

import com.cgy.common.constant.WareConstant;
import com.cgy.wodeyigui.ware.VO.ItemDonVo;
import com.cgy.wodeyigui.ware.VO.MergeVo;
import com.cgy.wodeyigui.ware.VO.PurchaseDoneVo;
import com.cgy.wodeyigui.ware.entity.PurchaseDetailEntity;
import com.cgy.wodeyigui.ware.service.PurchaseDetailService;
import com.cgy.wodeyigui.ware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgy.common.utils.PageUtils;
import com.cgy.common.utils.Query;

import com.cgy.wodeyigui.ware.dao.PurchaseDao;
import com.cgy.wodeyigui.ware.entity.PurchaseEntity;
import com.cgy.wodeyigui.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    WareSkuService wareSkuService;

    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceiveList(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);

    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        //首先判断PurchaseId()
        Long purchaseId = mergeVo.getPurchaseId();
        //如果没有创建新的采购单
        if(purchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseId=purchaseEntity.getId();//创建新的id
            this.save(purchaseEntity);
        }
        List<Long> items = mergeVo.getItems();
        //合并其实就是修改，
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(i);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());

            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

    @Override
    public void recived(List<Long> ids) {
        //1:确认当前采购单是新建或者已分配状态
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode()
                    || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()
            ) {
                return true;
            }
            return false;
        }).map(item -> {
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        //2:改变采购单的状态
        this.updateBatchById(collect);


        //3:改变采购项的状态
        collect.forEach((item)->{
            List<PurchaseDetailEntity> entities= purchaseDetailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = entities.stream().map(entity -> {
                PurchaseDetailEntity purchaseDetailEntity1 = new PurchaseDetailEntity();
                purchaseDetailEntity1.setId(entity.getId());
                purchaseDetailEntity1.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                return purchaseDetailEntity1;

            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        });
    }

    @Transactional
    @Override
    public void  done(PurchaseDoneVo purchaseDoneVo) {

        Long id = purchaseDoneVo.getId();

        //2:改变采购项状态
        Boolean flag = true;
        List<ItemDonVo> items = purchaseDoneVo.getItems();
        List<PurchaseDetailEntity> updates = new ArrayList<>();
        for (ItemDonVo item:items){
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if(item.getStatus()==WareConstant.PurchaseStatusEnum.HASRROR.getMsg()){
                flag = false;
                purchaseDetailEntity.setStatus(WareConstant.PurchaseStatusEnum.HASRROR.getCode());
            }else{
                purchaseDetailEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
                //3:将成功采购的进行入库\
                PurchaseDetailEntity Entity = purchaseDetailService.getById(item.getItemId());

                wareSkuService.addStocl(Entity.getSkuId(),Entity.getWareId(),Entity.getSkuNum());
            }
            //
            purchaseDetailEntity.setId(item.getItemId());

            updates.add(purchaseDetailEntity);
        }
            purchaseDetailService.updateBatchById(updates);
        //1:改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode():WareConstant.PurchaseStatusEnum.HASRROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);


    }

}